package com.magpiebridge.controller;

import com.magpiebridge.pojo.Matching;
import com.magpiebridge.pojo.User;
import com.magpiebridge.service.MatchingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class MatchingController {

    @Autowired
    private MatchingService matchingService;

    @GetMapping("/getAllMatching")
    public List<Matching> getAllMatching() {
        return matchingService.getAllMatching();
    }

    @GetMapping("/getMatchingById/{id}")
    public Matching getMatchingById(@PathVariable Integer id){
        return matchingService.getMatchingById(id);
    }

    @PostMapping("/insertMatching")
    public String insertMatching(@RequestBody Matching matching){
        Integer id = matching.getId();
        String city = matching.getCity();
        Integer age = matching.getAge();
        String sex = matching.getSex();
        Integer heig = matching.getHeig();
        Integer inmoney = matching.getInmoney();

        int result = matchingService.insertMatching(matching);
        if (result > 0){
            return "添加条件成功";
        } else {
            return "添加条件失败";
        }
    }

    @DeleteMapping("/deleteMatching/{id}")
    public  String deleteMatching(@PathVariable  Integer  id)  {
        try  {
            //  查询条件是否存在
            Matching  matching  =  matchingService.getMatchingById(id);
            if  (matching  ==  null)  {
                return  "条件不存在";
            }
            //  删除用户
            int  result  =  matchingService.deleteMatching(id);
            if  (result  >  0)  {
                //  更新数据库状态
                return  "条件删除成功";
            }  else  {
                return  "条件删除失败";
            }
        }  catch  (Exception  e)  {
            //  记录日志或警告
            //  TODO:  Log  deletion  information
            // 打印错误信息
            System.err.println(e.getMessage());
            return "系统错误";
        }
    }

    @PostMapping("/updateMatching/{id}")
    public String updateMatching(@PathVariable Integer id, @RequestBody Matching newMatching) {
        try {
            // 查询条件是否存在
            Matching matching = matchingService.getMatchingById(id);
            if (matching == null) {
                return "条件不存在";
            }
            // 更新用户信息
            matching.setId(newMatching.getId());
            matching.setCity(newMatching.getCity());
            matching.setAge(newMatching.getAge());
            matching.setSex(newMatching.getSex());
            matching.setHeig(newMatching.getHeig());
            matching.setInmoney(newMatching.getInmoney());
            int result = matchingService.updateMatching(matching);
            if (result > 0) {
                return "条件更新成功";
            } else {
                return "条件更新失败";
            }
        } catch (Exception e) {
            // 记录日志或警告
            // TODO: Log update information
            // 打印错误信息
            System.err.println(e.getMessage());
            return "系统错误";
        }
    }

}
